﻿// Written by Dale J Williams.
// Licensed under MPL 1.1 unless otherwise specified. 
// http://www.mozilla.org/MPL/MPL-1.1.html

package engineDJW.input {
	
	import flash.display.Stage;
	import flash.events.EventDispatcher;
	import flash.events.KeyboardEvent;
	
	/**
	 * ...
	 * @author Dale J Williams
	 */
	public class KeyboardManager extends EventDispatcher {
		
		private var _stage	:Stage;
		private var _keys	:Object;
		
		public function KeyboardManager($stage:Stage) {
			_stage = $stage;
			init();
		}
		
		private function init():void {
			_keys = { };			
			_stage.addEventListener(KeyboardEvent.KEY_DOWN, handleKeys);
			_stage.addEventListener(KeyboardEvent.KEY_UP, handleKeys);
		}
		
		private function handleKeys(e:KeyboardEvent):void {
			if (e.type == KeyboardEvent.KEY_DOWN) 
				_keys[e.keyCode] = e.keyCode;
			else delete _keys[e.keyCode];
			dispatchEvent(e);
		}
		
		public function isDown($keyCode:uint):Boolean {
			return _keys[$keyCode] != null;
		}
		
		public function get keysDown():Object {
			return _keys;
		}
		
	}

}